unit frmVideo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, RzPanel, uVideoCaptuer, uVLVolumeTrackBar, uDelphiBaseForm,
  uLangConst;

type
  TVideoFrom = class(TDelphiBaseForm)
    pnlBack: TRzPanel;
    pnlVideo: TPanel;
    pnlVolumeLeft: TPanel;
    pnlVolumeRight: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure pnlVideoClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    FVideoCapture: TVideoCaptuer;
    FOnVideoClick: TNotifyEvent;
    FLeftVolumeTrack: TVLVolumeTrackBar;
    FRightVolumeTrack: TVLVolumeTrackBar;
    procedure SetOnVideoClick(const Value: TNotifyEvent);
  public
    property VideoCapture: TVideoCaptuer read FVideoCapture;

    procedure SetShowHandle(AHwnd: Integer = -1);
    procedure SetPlayUrl(AURL: string);
    procedure Start();
    procedure Stop();
    procedure SetAutoReconnect(AAutoReconnect: Boolean);

    procedure SetAudioMute(AMute: Boolean);

    procedure ShowVideo(AShow: Boolean);
    procedure ShowAudio(AShow: Boolean);
    
    procedure SetSelect(ASelected: Boolean);
    property OnVideoClick: TNotifyEvent read FOnVideoClick write SetOnVideoClick;

    function StartEncoder(AFileName: PAnsiChar): Integer;
    procedure StopEncoder();
    procedure ChangeLanguage(ALanguageIniFile: string); override;
  end;

var
  VideoFrom: TVideoFrom;

implementation

{$R *.dfm}

procedure OnAudioVolume(AChannel: LongInt; AValue: LongInt; Context: LongInt) stdcall;
var
  form: TVideoFrom;
begin
  form := TVideoFrom(Context);

  if AChannel = 0 then
  begin
    form.FLeftVolumeTrack.PeakValue := AValue;
  end
  else
    form.FRightVolumeTrack.PeakValue := AValue;
end;

procedure TVideoFrom.ChangeLanguage(ALanguageIniFile: string);
begin
  inherited;

end;

procedure TVideoFrom.FormCreate(Sender: TObject);
begin
  FVideoCapture := TVideoCaptuer.Create;
  FOnVideoClick := nil;

  FLeftVolumeTrack := TVLVolumeTrackBar.Create(Self);
  FLeftVolumeTrack.Parent := pnlVolumeLeft;
  FLeftVolumeTrack.Align := alClient;
  FLeftVolumeTrack.Show;

  FRightVolumeTrack := TVLVolumeTrackBar.Create(Self);
  FRightVolumeTrack.Parent := pnlVolumeRight;
  FRightVolumeTrack.Align := alClient;
  FRightVolumeTrack.Show;
end;

procedure TVideoFrom.FormDestroy(Sender: TObject);
begin
  FVideoCapture.Destroy();
  FLeftVolumeTrack.Free;
  FRightVolumeTrack.Free;
end;

procedure TVideoFrom.FormShow(Sender: TObject);
begin
  TranslateConsts(getLanguageFileName());
  ChangeLanguage(getLanguageFileName());
end;

procedure TVideoFrom.SetPlayUrl(AURL: string);
begin
  FVideoCapture.SetURLEx(AURL);
end;

procedure TVideoFrom.pnlVideoClick(Sender: TObject);
begin
  if Assigned(FOnVideoClick) then
    FOnVideoClick(nil);
end;

procedure TVideoFrom.SetAudioMute(AMute: Boolean);
begin
  FVideoCapture.SetAudioMute(AMute);
end;

procedure TVideoFrom.SetAutoReconnect(AAutoReconnect: Boolean);
begin
  FVideoCapture.SetAutoReconnect(AAutoReconnect);
end;

procedure TVideoFrom.SetOnVideoClick(const Value: TNotifyEvent);
begin
  FOnVideoClick := Value;
end;

procedure TVideoFrom.SetSelect(ASelected: Boolean);
begin
  if ASelected then
    pnlBack.BorderColor := $000080FF
  else
    pnlBack.BorderColor := clBlack;
end;

procedure TVideoFrom.SetShowHandle(AHwnd: Integer = -1);
begin
  if (-1 = AHwnd) then
    FVideoCapture.SetDrawHWNDEx(pnlVideo.Handle)
  else
    FVideoCapture.SetDrawHWNDEx(AHwnd);
end;

procedure TVideoFrom.ShowAudio(AShow: Boolean);
begin
  if Self.Visible then
  begin
    pnlVolumeLeft.Visible := AShow;
    pnlVolumeRight.Visible := AShow;
  end;
end;

procedure TVideoFrom.ShowVideo(AShow: Boolean);
begin
  if Self.Visible then
    FVideoCapture.Show(AShow);
end;

procedure TVideoFrom.Start;
begin
  if Self.Visible then
  begin
    FVideoCapture.Start();
    FVideoCapture.SetAnalyseCallBack(@OnAudioVolume, Integer(Self));
  end;
end;

function TVideoFrom.StartEncoder(AFileName: PAnsiChar): Integer;
begin
  FVideoCapture.StartEncoderEx(AFileName);
end;

procedure TVideoFrom.Stop;
begin
  FVideoCapture.Stop();
end;

procedure TVideoFrom.StopEncoder;
begin
  FVideoCapture.StopEncoderEx();
end;

end.
